package cn.yangyumanage.adapter;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chauthai.swipereveallayout.SwipeRevealLayout;

import java.util.List;

import cn.yangyumanage.EditActivity;
import cn.yangyumanage.R;
import cn.yangyumanage.bean.Film;
import cn.yangyumanage.callback.DataCallBack;
import cn.yangyumanage.http.HttpClient;
import cn.yangyumanage.model.FilmManageHttpModel;
import cn.yangyumanage.util.ToastUtil;

public class FilmManageRvAdapter extends RecyclerView.Adapter<FilmManageRvAdapter.MyViewHolder> {
    private Context context;
    private List<Film> films;
    private final Bundle bundle = new Bundle();
    private final FilmManageHttpModel httpModel = new FilmManageHttpModel();

    public FilmManageRvAdapter(Context context, List<Film> films) {
        this.context = context;
        this.films = films;
    }

    @NonNull
    @Override
    public FilmManageRvAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(this.context).inflate(R.layout.film_manage_rv_item, parent, false);
        return new MyViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull FilmManageRvAdapter.MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
        Glide.with(context).load(films.get(position).getPostUrl()).into(holder.iv_post);
        holder.tv_name.setText(films.get(position).getName());
        holder.tv_aka.setText("又名：" + films.get(position).getAka());
        holder.tv_plot.setText(films.get(position).getPlot());
        holder.srl_item.setLockDrag(true);
        holder.srl_item.close(true);
        holder.ll_item.setOnClickListener(new View.OnClickListener() {
            boolean flag = false;
            @Override
            public void onClick(View v) {
                if (!flag) {
                    holder.srl_item.open(true);
                    flag = true;
                } else {
                    holder.srl_item.close(true);
                    flag = false;
                }
            }
        });
        holder.tv_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, EditActivity.class);
                bundle.clear();
                Film film = films.get(position);
                bundle.putSerializable("filmBean", film);
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
        holder.tv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建提醒对话框的构建器
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                //设置对话框的内容文本
                builder.setMessage("真的要删除该影片吗？");
                //设置对话框的肯定按钮文本及其点击监听器
                builder.setPositiveButton("取消", null);
                //设置对话框的否定按钮文本及其点击监听器
                builder.setNegativeButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Integer id = films.get(position).getId();
                        deleteById(id, position);
                    }
                });

                //根据建造器构建提醒对话框对象
                AlertDialog dialog = builder.create();
                //显示提醒对话框
                dialog.show();
                dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.RED);
            }
        });
    }

    @Override
    public int getItemCount() {
        return this.films.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {


        private final ImageView iv_post;
        private final TextView tv_name;
        private final TextView tv_aka;
        private final TextView tv_plot;
        private final TextView tv_delete;
        private final TextView tv_edit;
        private final SwipeRevealLayout srl_item;
        private final LinearLayout ll_item;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_post = itemView.findViewById(R.id.iv_post);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_aka = itemView.findViewById(R.id.tv_aka);
            tv_plot = itemView.findViewById(R.id.tv_plot);
            tv_delete = itemView.findViewById(R.id.tv_delete);
            tv_edit = itemView.findViewById(R.id.tv_edit);
            srl_item = itemView.findViewById(R.id.srl_item);
            ll_item = itemView.findViewById(R.id.ll_item);
        }
    }

    //通过id删除影片
    private void deleteById(Integer id, int position) {
        httpModel.deleteFilmById(id, new DataCallBack() {
            @Override
            public void getData(Object data) {
                Boolean flag = (Boolean) data;
                HttpClient.handler.post(new Runnable() {
                    @SuppressLint("NotifyDataSetChanged")
                    @Override
                    public void run() {
                        if (flag) {
                            ToastUtil.show(context, "删除成功");
                            films.remove(position);
                            notifyDataSetChanged();
                        } else {
                            ToastUtil.show(context, "删除失败");
                        }
                    }
                });
            }
        });
    }
}
